import java.io.*;

public class t {

    /**
     * 遍历文件夹下的所有文件进行文本替换
     *
     * @param fileDir
     * @param oldStr
     * @param replaceStr
     */
    public static void iteratorDirectory(String fileDir, String oldStr, String replaceStr) {
        File file = new File(fileDir);
        if (file.isDirectory()) {
            String[] fileList = file.list();
            for (int i = 0; i < fileList.length; i++) {
                iteratorDirectory(fileDir + File.separator + fileList[i], oldStr, replaceStr);
            }
        } else {
            replaceTxtByStr(fileDir, oldStr, replaceStr);
        }
    }

    /**
     * 替换文件中的字符串
     *
     * @param filePath
     * @param oldStr
     * @param replaceStr
     */
    public static void replaceTxtByStr(String filePath, String oldStr, String replaceStr) {
        int len = oldStr.length();
        StringBuffer tempBuf = new StringBuffer();
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();


            // 按行读取，仅替换每行第一个匹配的字符串
            //while ((temp = br.readLine()) != null) {
            //    if (temp.contains(oldStr)) {
            //        int index = temp.indexOf(oldStr);
            //        tempBuf.append(temp);
            //        tempBuf.replace(index, index + len, replaceStr);
            //        buf.append(tempBuf);
            //        tempBuf.setLength(0);
            //    } else {
            //        buf.append(temp);
            //    }
            //    buf = buf.append(System.getProperty("line.separator"));
            // }

            // 替换所有匹配的字符串
            for (String temp = null; (temp = br.readLine()) != null; temp = null) {
                if (temp.indexOf(oldStr) != -1) {
                    temp = temp.replace(oldStr, replaceStr);
                }
                buf.append(temp);
                buf.append(System.getProperty("line.separator"));
            }

            br.close();
            FileOutputStream fos = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void changeFileName(String path,String oldStr, String replaceStr) {
        File file = new File(path);

        if (file.exists()) {
            File[] files = file.listFiles();

            if (null == files || files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        changeFileName(file2.getAbsolutePath(),oldStr,replaceStr);
                    } else {
                        System.out.println("文件:" + file2.getAbsolutePath());
                        String filePath = file2.getAbsolutePath();
                        String fileName = filePath.substring(0, filePath.lastIndexOf("\\")) + "\\"  +file2.getName().replace(oldStr,replaceStr);
                        File oriFile = new File(filePath);
                        boolean b = oriFile.renameTo(new File(fileName));
                        System.out.println(b);
                    }
                    String filePath = file2.getAbsolutePath();
                    File newFile = new File(filePath.substring(0, filePath.lastIndexOf("\\")) + "\\"  +file2.getName().replace(oldStr,replaceStr));
                    file2.renameTo(newFile);
                }
            }
        } else {
            System.out.println("该路径不存在");
        }
    }

    public static void main(String[] args) {
        changeFileName("E:\\ygg\\test\\cpjar\\out\\ygg-file","YggFile", "YggWebsocket");
//        iteratorDirectory("E:\\ygg\\test\\cpjar\\src\\ygg-file", "ruoyi", "ygg");
//        iteratorDirectory("H:\\py\\pt\\replace", "RuoYi", "Ygg");
//        iteratorDirectory("H:\\py\\pt\\replace", "若依", "YGG");
    }
}
